import { FormRules } from 'element-plus'

const FormValidateRulesHost: FormRules = {
  name: [{ required: true, message: '请输入服务器主机连接配置名称', trigger: 'blur' }],
  priority: [{ required: true, message: '请输入当前服务器配置的优先级', trigger: 'blur' }],
  host: [
    { required: true, message: '请输入服务器主机地址', trigger: 'blur' },
    {
      validator: (_, value, callback) => {
        if (value === 'localhost') callback()
        // 如果是 IPv4 地址
        else if (/^(\d+\.){3}(\d+)$/.test(value)) {
          if (!RegUtil.isIpv4Legal(value)) callback(new Error('请输入正确的服务器主机 IPv4 地址'))
          else callback()
        } else {
          if (!RegUtil.isDomainLegal(value)) callback(new Error('请输入正确的服务器主机域名地址'))
          else callback()
        }
      },
      trigger: 'blur'
    }
  ],
  port: [{ required: true, message: '请输入服务器主机 SSH 连接端口号', trigger: 'blur' }],
  username: [{ required: true, message: '请输入服务器主机 SSH 连接用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入服务器主机 SSH 连接密码', trigger: 'blur' }]
}

export const FormValidateRules = {
  [HostConnConfTypeConst.DIR]: FormValidateRulesDir,
  [HostConnConfTypeConst.HOST]: FormValidateRulesHost
}
